import { _decorator, Component, director, EditBox, Label, Node, ProgressBar, resources, Sprite, Toggle, tween, Vec2, Vec3 } from 'cc';
import { UIManager } from '../UIManager';
import { LanauageManager } from '../LanauageManager';
import { GameData } from '../GameData';
import { HttpClient } from '../net/HttpClient';
import { EventManager } from '../EventManager';
import { EventManger } from '../../appComon/scripts/EventManger';
import { PREVIEW } from 'cc/env';
const { ccclass, property } = _decorator;

@ccclass('login')
export class login extends Component {

    @property(ProgressBar)  
    progre: ProgressBar = null; 

    @property(Toggle)  
    checkTog: Toggle = null; 

    //--------------登录界面---------------------

    @property(Node)  
    loginNode: Node = null;
    
    @property(EditBox)  
    loginPhoneEdi: EditBox = null;

    @property(EditBox)  
    loginPassEdi: EditBox = null;

    //----------注册界面-------------------

    @property(Node)  
    regiNode: Node = null; 

    @property(EditBox)  
    regiPhoneEdi: EditBox = null;

    @property(EditBox)  
    regiCodeEdi: EditBox = null;

    @property(EditBox)  
    regiPassEdi: EditBox = null;

    @property(EditBox)  
    regiPass2Edi: EditBox = null;

    @property(EditBox)  
    regiNameEdi: EditBox = null;

    @property(EditBox)  
    regiIDCardEdi: EditBox = null;

    @property(EditBox)  
    regiInivteEdi: EditBox = null;

    @property(Node)  
    rigicodeGetBtn: Node = null;


    //----------密码重置界面-------------------

    @property(Node)  
    resetNode: Node = null; 

    @property(EditBox)  
    resetPhoneEdi: EditBox = null;

    @property(EditBox)  
    resetCodeEdi: EditBox = null;

    @property(EditBox)  
    resetPassEdi: EditBox = null;

    @property(EditBox)  
    resetPass2Edi: EditBox = null;

    @property(Node)  
    resetCodeGetBtn: Node = null;

    public static self:any;

    onEnable() {
        director.preloadScene("main");
        // this.resetNode.active = false;
        // this.regiNode.active = false;
        // this.loginNode.active = true;

        // if(PREVIEW){
        //     this.loginPhoneEdi.string = "18137787867";
        //     this.loginPassEdi.string = "111";

        //     this.checkTog.isChecked = true;
        // }

        // this.regiInivteEdi.string = "000000"
        // login.self = this;
        // this.progre.progress = 0;
        // tween(this.progre)
        // .to(3,{progress:0.95}, { easing: 'quadIn' })
        // .start();
        // resources.preloadDir("ui", ()=>{
        //     console.log("preload success")
        //     //director.loadScene("game")  
        //     director.preloadScene("game", this.onComplete)
        //  })
    }


    onComplete() {  
        console.log("preload game success")
        tween(login.self.progre)
        .to(0.2,{progress:0.98})
        .call(()=>{
            console.log("load game success")
            director.loadScene("game") 
        })
        .to(0.1,{progress:1})
        .to(50,{progress:1})
        .start();
    } 

    onBtnClick(event:Event, str:string){
        var self = this;
        if (str == ""){
           
        }
        else if (str == "openRegisNode"){
            this.regiNode.active = true;
        }
        else if (str == "openResetNode"){
            this.resetNode.active = true;
        }
        else if (str == "loginBtn"){         //登录账号
             //临时登录
            if (!this.checkTog.isChecked){
                UIManager.showTip("请先勾选选中框");
                return;
            }
            director.loadScene("main") 
            // if (this.loginPhoneEdi.string == "" || this.loginPassEdi.string == ""){
            //     UIManager.showTip("参数不能为空");
            //     return
            // }
            // HttpClient.getInstance().sendLogin(this.loginPhoneEdi.string, this.loginPassEdi.string, ()=>{
            //     //登录成功
            //     director.loadScene("main") 
            // })
        }
        else if (str == "regisBtn"){            //注册账号
            if (this.regiPhoneEdi.string == "" || this.regiCodeEdi.string == ""|| this.regiPassEdi.string == ""|| this.regiPass2Edi.string == ""){
                UIManager.showTip("参数不能为空");
                return
            }
            if (this.regiPassEdi.string != this.regiPass2Edi.string){
                UIManager.showTip("两次密码不一致");
                return
            }

            let invitCode = this.regiInivteEdi.string == ""?"000000":this.regiInivteEdi.string;

            HttpClient.getInstance().sendRegis(this.regiPhoneEdi.string, this.regiCodeEdi.string,this.regiPassEdi.string,this.regiNameEdi.string, this.regiIDCardEdi.string, invitCode, ()=>{
                //注册成功
                UIManager.showTip("恭喜您注册成功,请前往登录");
                self.regiNode.active = false;
            })

        }
        else if (str == "resetBtn"){            //重置密码
            if (this.resetPhoneEdi.string == "" || this.resetCodeEdi.string == ""|| this.resetPassEdi.string == ""|| this.resetPass2Edi.string == ""){
                UIManager.showTip("参数不能为空");
                return
            }
            if (this.resetPassEdi.string != this.resetPass2Edi.string){
                UIManager.showTip("两次密码不一致");
                return
            }

            HttpClient.getInstance().sendResetPass(this.resetPhoneEdi.string, this.resetCodeEdi.string,this.resetPassEdi.string,this.resetPass2Edi.string, ()=>{
                //注册成功
                UIManager.showTip("密码重置成功,请前往登录");
                self.resetNode.active = false;
            })

        }
        else if (str == "codeGetBtnRegis"){         
            if (this.regiPhoneEdi.string == "" ){
                UIManager.showTip("参数不能为空");
                return
            }
            HttpClient.getInstance().sendGetCode(this.regiPhoneEdi.string, ()=>{
                UIManager.showTip("验证码发送成功");
                this.rigicodeGetBtn.active = false;
                this.scheduleOnce(()=>{
                    this.rigicodeGetBtn.active = true;
                },60)
            })
        }

        else if (str == "codeGetBtnReset"){
            if (this.resetPhoneEdi.string == "" ){
                UIManager.showTip("参数不能为空");
                return
            }
            HttpClient.getInstance().sendGetCode(this.resetPhoneEdi.string, ()=>{
                UIManager.showTip("验证码发送成功");
                this.resetCodeGetBtn.active = false;
                this.scheduleOnce(()=>{
                    this.rigicodeGetBtn.active = true;
                },60)
            })
        }
        

        //director.loadScene("game") 
    }

  
}

